<?php

namespace App\Http\Controllers\Cruise\Web;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\FeishuWebAuth;
use App\Jobs\Export\Export;
use App\Models\Activity\ActivityModel;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\Cruise\Check\CruiseCheckReportService;
use App\Service\Cruise\Mission\CheckInputService;
use App\Support\CustomForm\AttributeCruiseSupport;
use App\Support\CustomForm\AttributeSupport;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use PhpParser\Node\Attribute;

/**
 * 巡检报告
 */
class CruiseCheckReportController extends BaseController
{
    public function lists(Request $request): JsonResponse
    {
        $pageSize      = (int)$request->get('pageSize', 20);
        $pageNum       = (int)$request->get('pageNum', 1);
        $status        = (int)$request->get('status', 0);
        $startDateTime = (string)$request->get('startDateTime', '');
        $endDateTime   = (string)$request->get('endDateTime', '');
        $dealUid       = (int)$request->get('dealUid');
        $templateName  = (string)$request->get('templateName'); // 模板名称
        $shop          = (string)$request->get('shop'); // 门店编号、名称
        $sysTemplate   = (int)$request->get('sysTemplate'); // 常规、专项
        $cruiseCheckRectifyStatus   = (string)$request->get('cruiseCheckRectifyStatus', '');
        $templateType    = (int)$request->get('templateType', CruiseTemplateModel::TYPE_3);
        $cruiseCheckCompanyIds = (string)$request->get('cruiseCheckCompanyIds');
        $cruiseCheckCompanyIds = $cruiseCheckCompanyIds ? explode(',', $cruiseCheckCompanyIds) : [];
        $inputType = (string)$request->get('inputType', AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK);
        $taskName = (string)$request->get('taskName', '');
        $templateIds = (string)$request->get('templateIds', '');
        $templateIds = $templateIds ? explode(',',$templateIds) : [];
        $export = (int)(bool)$request->get('export', 0);
        $userId = FeishuWebAuth::$userId;

        if ($export) {

            if ($inputType == AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK_BIOLOGY) {
                $fileNamePrefix = '巡检报告（生物防治）';
            } else {
                $fileNamePrefix = '巡检报告';
            }

            $fileName = vsprintf("%s_%s", [
                $fileNamePrefix,
                date("Y_m_d_H_i_s", time())
            ]);

            if (config('app.env') == 'dev') {
                Config::set('event.connection', 'sync');
            }

            dispatch(
                (new Export(
                    $fileName, Export::TYPE_CRUISE_CHECK_REPORT, [
                        'data'      => [
                            'startDateTime'            => $startDateTime,
                            'endDateTime'              => $endDateTime,
                            'dealUid'                  => $dealUid,
                            'templateName'             => $templateName,
                            'shop'                     => $shop,
                            'sysTemplate'              => $sysTemplate,
                            'status'                   => $status,
                            'templateType'             => $templateType,
                            'cruiseCheckCompanyIds'    => $cruiseCheckCompanyIds,
                            'cruiseCheckRectifyStatus' => $cruiseCheckRectifyStatus,
                            'inputType'                => $inputType,
                            'taskName'                 => $taskName,
                            'templateIds'               => $templateIds,
                        ],
                        'startTime' => 0,
                        'endTime'   => time(),
                        'userId'    => $userId,
                        'filename' => $fileName,
                    ]
                ))
            )->onQueue(Export::QUEUE_NAME);

            return $this->success();
        }

        $data = (new CruiseCheckReportService)->lists(
            $startDateTime ? strtotime($startDateTime) : 0,
            $endDateTime ? strtotime($endDateTime) : 0,
            $dealUid,
            $templateName,
            $shop,
            $sysTemplate,
            $pageSize,
            $pageNum,
            $status,
            $templateType,
            $cruiseCheckCompanyIds,
            $cruiseCheckRectifyStatus,
            $inputType,
            $taskName,
            $templateIds,
            $userId,
            $export
        );

        return $this->success($data);

    }

    public function detail(Request $request, int $id)
    {
        $data = (new CruiseCheckReportService())->detail($id);

        return $this->success($data);
    }
}
